package com.framework.admin.controller;

import com.framework.admin.model.SysDict;
import com.framework.admin.service.IDictService;
import com.framework.common.domain.ParamPage;
import com.framework.common.domain.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping( "/dict")
@Api(value = "dict", tags = "字典管理")
public class DictController {
    @Autowired
    IDictService dictService;

    /**
     * 获取字典列表
     */
    @ResponseBody
    @ApiOperation(value = "获取字典列表")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<Page<SysDict>> list(SysDict dict, ParamPage page) {
        PageRequest request = PageRequest.of(page.getPageNo() - 1, page.getPageSize());
        Page<SysDict> tree = dictService.getDictList(dict, request);
        return ResultData.data(tree);
    }

    /**
     * 保存数字字典
     */
    @ResponseBody
    @ApiOperation(value = "保存数字字典")
    @PostMapping(value = "/save")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<String> save(@RequestBody SysDict dict) {
        dictService.save(dict);
        return ResultData.success();
    }

    /**
     * 删除数字字典
     */
    @ResponseBody
    @ApiOperation(value = "删除数字字典")
    @PostMapping(value = "/del")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<String> del(@RequestBody List<String> ids) {
        dictService.delete(ids);
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "按code获取字典列表")
    @GetMapping(value = "/children")
    public ResultData<List<SysDict>> findByPcode(@RequestParam @ApiParam(value="字典编码") String code) {
        return ResultData.data(dictService.findByPcode(code));
    }
}
